package com.mldong.config;

import com.mldong.base.SwaggerDocket;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2WebMvc;

import java.util.List;

@Configuration
@EnableSwagger2WebMvc
@RequiredArgsConstructor
public class Knife4jConfiguration {

//    @Bean(value = "defaultApi2")
//    public Docket defaultApi2() {
//        Docket docket=new Docket(DocumentationType.SWAGGER_2)
//                .apiInfo(new ApiInfoBuilder()
//                        .title("mldong-course-boot APIs")
//                        .description("# mldong-course-boot APIs")
//                        .termsOfServiceUrl("http://www.mldong.com/")
//                        .version("1.0")
//                        .build())
//                //分组名称
//                .groupName("V1.0")
//                .select()
//                //这里指定Controller扫描包路径
//                .apis(RequestHandlerSelectors.basePackage("com.mldong.modules.sys.controller"))
//                .paths(PathSelectors.any())
//                .build();
//        return docket;
//    }
    /**
     * 动态分组
     */
    private final ApplicationContext applicationContext;
    private final List<SwaggerDocket> swaggerDocketList;
    @Bean
    public List<SwaggerDocket> docketList() {
        ConfigurableApplicationContext context = (ConfigurableApplicationContext)applicationContext;
        DefaultListableBeanFactory beanFactory = (DefaultListableBeanFactory)context.getBeanFactory();
        for(int i=0,len=swaggerDocketList.size();i<len;i++) {
            SwaggerDocket swaggerDocket = swaggerDocketList.get(i);
            BeanDefinitionBuilder beanDefinitionBuilder = BeanDefinitionBuilder.rootBeanDefinition(Docket.class);
            beanDefinitionBuilder
                    .addConstructorArgValue(DocumentationType.SWAGGER_2);
            BeanDefinition beanDefinition=beanDefinitionBuilder.getBeanDefinition();
            beanFactory.registerBeanDefinition("docketApi"+i, beanDefinition);
            Docket docket = this.applicationContext.getBean("docketApi"+i, Docket.class);
            docket.apiInfo(new ApiInfoBuilder()
                    .title("mldong-course-boot APIs")
                    .description("# mldong-course-boot APIs")
                    .termsOfServiceUrl("http://www.mldong.com/")
                    .version("1.0")
                    .build())
                    //分组名称
                    .groupName(swaggerDocket.groupName())
                    .select()
                    //这里指定Controller扫描包路径
                    .apis(RequestHandlerSelectors.basePackage(swaggerDocket.basePackage()))
                    .paths(PathSelectors.any())
                    .build();
        }
        return null;
    }
}